﻿
#region Usings

using PolyGameEngine.GamePieces;


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

#endregion

namespace PolyGameEngine.Tools
{
    public class InputMan
    {
        public static KeyboardState PreviousKeyboardState = Keyboard.GetState();
        public static KeyboardState currentKeyboardState = Keyboard.GetState();

        public static MouseState previousMouseState = Mouse.GetState();
        public static MouseState currentMouseState = Mouse.GetState();
        public static Vector2 MousePositionGrid
        {
            get
            {
                return new Vector2((((currentMouseState.X - (currentMouseState.X) % 50)) + (GamePiece._ScreenOffset.X % 50)) + 35,
                    (((currentMouseState.Y - (currentMouseState.Y) % 50)) + (GamePiece._ScreenOffset.Y % 50)) + 35);

            }
        }

        public static Vector2 MousePosition
        {
            get
            {
                return new Vector2(currentMouseState.X, currentMouseState.Y);
            }
        }

        public static bool IsKeyDown(Keys Key)
        {
            return (currentKeyboardState.IsKeyDown(Key));
        }

        public static bool IsKeyUp(Keys Key)
        {
            return (currentKeyboardState.IsKeyUp(Key));
        }

        public static bool OnRelease(Keys Key)
        {
            return IsKeyUp(Key) && PreviousKeyboardState.IsKeyDown(Key);
        }
        
        //returns true once the key is pressed, If the button stays pressed it
        //returns false until it has been depressed and pressed again.
        public static bool OnPress(Keys Key)
        {
            return IsKeyDown(Key) && PreviousKeyboardState.IsKeyUp(Key);
        }
        
        //returns true one time when the button is depressed after it was pressed.
        public static bool onLeftMouseRelease()
        {
            return currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed;
        }

        public static bool isMouseLeftDown()
        {
            return currentMouseState.LeftButton == ButtonState.Pressed;
        }
        public static bool isMouseMiddleDown()
        {
            return currentMouseState.MiddleButton == ButtonState.Pressed;
        }
        public static bool isMouseRightDown()
        {
            return currentMouseState.RightButton == ButtonState.Pressed;
        }
        public static bool onLeftMousePress()
        {
            return currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released;
        }

        public static bool onRightMouseRelease()
        {
            return currentMouseState.RightButton == ButtonState.Released && previousMouseState.RightButton == ButtonState.Pressed;
        }
        public static bool onRightMousePress()
        {
            return currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Released;
        }
        public static bool onMiddleMousePress()
        {
            return currentMouseState.MiddleButton == ButtonState.Pressed && previousMouseState.MiddleButton == ButtonState.Released;
        }


        public static int scrollDirection()
        {
            if (currentMouseState.ScrollWheelValue > previousMouseState.ScrollWheelValue)
            {
                return 1;
            }
            else if (currentMouseState.ScrollWheelValue < previousMouseState.ScrollWheelValue)
            {
                return -1;
            }
            else
                return 0;
        }

        public static void Update()
        {
            PreviousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();
            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
        }

    }
}
